package com.sam.im.heiqun.uis.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.melink.bqmmsdk.widget.BQMMMessageText;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.ColactionEntity;
import com.sam.im.heiqun.entities.MsgEntity;
import com.sam.im.heiqun.entities.VoiceEntity;
import com.sam.im.heiqun.uis.adapters.holder.ColatTextViewHolder;
import com.sam.im.heiqun.uis.widgets.MediaManager;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.ActivityUtil;
import com.yuyh.library.utils.GlideUtils;
import com.yuyh.library.utils.ScreenUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangguodong on 2018/3/5.
 */

public class ColactionDetailActivity extends BaseSwipeBackActivity {

    @BindView(R.id.pre_v_back)
    ImageView preVBack;
    @BindView(R.id.image_head)
    ImageView image_head;
    @BindView(R.id.img_voice)
    ImageView img_voice;
    @BindView(R.id.txt_name)
    TextView txt_name;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.content)
    BQMMMessageText bqmmMessageText;
    @BindView(R.id.linear_txt)
    LinearLayout linear_txt;
    @BindView(R.id.linear_audio)
    LinearLayout linear_audio;
    @BindView(R.id.image_group)
    RelativeLayout image_group;

    private ColactionEntity.ListBean listBean ;
    private boolean isAudioShow = false ;
    String fileUrl = "";

    public static void start(Activity activity, ColactionEntity.ListBean listBean){
        Intent intent = new Intent(activity, ColactionDetailActivity.class);
        intent.putExtra("listBean", listBean);
        activity.startActivity(intent);
    }

    @Override
    protected String getTitleText() {
        return "详情";
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {

        listBean = (ColactionEntity.ListBean) getIntent().getSerializableExtra("listBean");
        if (null!=listBean){
            txt_name.setText(listBean.getFromName());
            GlideUtils.loadCircleImage(ColactionDetailActivity.this, listBean.getFromHeadUrl(), image_head);
            switch (getItemType(listBean)) {
                case 1://文字
                    linear_txt.setVisibility(View.VISIBLE);

                    try {
                        MsgEntity msgEntity = new Gson().fromJson(listBean.getContent(), MsgEntity.class);
                        if (msgEntity.getMsgCodes() != null && !"".equals(msgEntity.getMsgCodes())) {
                            //大表情
                            try {
                                JSONArray array = new JSONArray(msgEntity.getMsgCodes());
                                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                        LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(0, 0, ScreenUtil.dp2px(10), 0);
                                bqmmMessageText.setLayoutParams(params);
                                bqmmMessageText.setBackgroundResource(R.color.chat_backgroud);
                                bqmmMessageText.showMessage(msgEntity.getMsgString(), msgEntity.getMsgType(), array);
                                bqmmMessageText.setBigEmojiShowSize(ScreenUtil.dp2px(100));
                                // Log.i("info","iiii=="+array);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            //图文混排
                            bqmmMessageText.setText(msgEntity.getMsgString());
                        }

                    } catch (Exception e) {
                        bqmmMessageText.setText(listBean.getContent());
                    }

                    break;
                case 3://语音
                    linear_audio.setVisibility(View.VISIBLE);
                    try {
                        VoiceEntity msgEntity = new Gson().fromJson(listBean.getContent(),VoiceEntity.class);
                        if (msgEntity!=null) {
//                    voiceTime = Float.parseFloat(msgEntity.getTime());
                            tv_time.setText(msgEntity.getTime()+ "\"");
                            fileUrl = msgEntity.getUrl();
                        }
                    }catch (Exception e){e.printStackTrace();}
                    break;
            }
        }

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_colaction_detail;
    }

    @OnClick({R.id.pre_v_back, R.id.linear_audio})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.pre_v_back: {
                this.finish();
                break;
            }
            case R.id.linear_audio: {
                try {
                    if (isAudioShow)audioStop();else audioStart(fileUrl);
                }catch (Exception e){e.printStackTrace();}

                break;
            }
        }
    }

    private void audioStart(String voicePath){
        try {
            isAudioShow = true ;
            AnimationDrawable drawable;
            img_voice
                    .setBackgroundResource(R.drawable.receiver_voice_node_playing003);
            img_voice
                    .setBackgroundResource(R.drawable.voice_play_receiver);
            drawable = (AnimationDrawable) img_voice
                    .getBackground();
            drawable.start();

            MediaManager.playSound(ColactionDetailActivity.this, voicePath,
                    new MediaPlayer.OnCompletionListener() {

                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            audioStop();
                        }
                    }, AudioManager.STREAM_MUSIC);
        }catch (Exception e){e.printStackTrace();}

    }
    private void audioStop(){
        try {
            img_voice
                    .setBackgroundResource(R.drawable.receiver_voice_node_playing003);
            MediaManager.pause();
            isAudioShow = false ;
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public int getItemType(ColactionEntity.ListBean listBean) {
        if (null != listBean) {
            try {
                return Integer.parseInt(listBean.getCategory());
            }catch (Exception e){e.printStackTrace();}

        }
        return 0;
    }

}
